package com.oocl.homework.crm.command;

import com.oocl.homework.crm.datastruct.Executeable;
import com.oocl.homework.crm.model.Filterable;

public class OrderCommand<T> extends Command<T> {
	
	private String orderAttritute;

	public OrderCommand(String command) {
		super(command);
	}
	
	@Override
	public void filter(Filterable filterable) {
		super.filter(filterable);
		if(result.getCode()==-1){
			return ;
		}else{
				switch (command) {
				case "O id":
					orderAttritute = "id";
					break;
				case "O name":
					orderAttritute = "name";
					break;
				default:
					result.setCode(-1);
					result.setMessage("Unrecognized O command");
					break;
				}
			
		}
		
	}

	@Override
	public void executeCommand(Executeable<T> executeable,T t) {
		super.executeCommand(executeable, t);
		if (orderAttritute.equals("id")) {
			try {
				System.out.println(executeable.sortById());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if (orderAttritute.equals("name")) {
			try {
				System.out.println(executeable.sortByName());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
	}
	
	@Override
	public void display() {
		super.display();
		CommandDoc.printOrderCommandHelp();
		
	}


}
